package pl.elka.simulator.gui.swt.menu;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import pl.elka.simulator.mediator.SimulationFasade;
import pl.elka.simulator.persistance.IPersister;
import pl.elka.simulator.persistance.xml.XMLPersister;

public class SaveSelection extends SelectionAdapter {

	private IPersister persister = new XMLPersister();
	
	private Shell shell;
	public SaveSelection(Shell shell){
		this.shell = shell;
	}
	
	@Override
	public void widgetSelected(SelectionEvent e) {
		FileDialog fd = new FileDialog(shell, SWT.SAVE);
		fd.setText("Zapisz");
		fd.setFilterPath("C:/");
		String[] filterExt = { "*." + persister.getPersistanceType().getFileType()};
		fd.setFilterExtensions(filterExt);
		String selected = fd.open();	  
		if(selected!=null){
			persister.persist(SimulationFasade.getInstance().getSimulator(), selected);			
		}
	}
}
